<?php


$i = 10;

while($i < 200)

{
	$base_url = "http://www.jyeoo.com/math/report/search?g=8&t=2&k=&o=2&d=1&p=$i";
	$content = file_get_contents($base_url);
	file_put_contents( dirname(__FILE__ ) . '/log', $title . '{br}'. $content . '{br}');

	preg_match_all('/<div class="R01"><a href="(.*?)" target="_blank" class="f14">(.*?)<\/a><\/div>/i', $content, $matches);

	if(is_array($matches))
	{
		$hrefs = $matches[1];
		$titles = $matches[2];

		foreach($hrefs as $k=>$v)
		{
			//if($k  5)  continue;
			$title = $titles[$k];
			$message = get_message('http://www.jyeoo.com'. $v);

			$data = array('title'=>$title, 'message'=>$message, 'fid'=>96);
			$ret = https_post('http://www.hbebbs.com/http_discuz_publish.php', $data, '', false);
			
			var_dump($ret);
			
			//exit(0);
			//echo $title . "<br>";
		}
		
		//header("location: ./" . basename(__FILE__) . '?ii=' . ($i++) ); 
		
	}
$i++;
}



function get_message($v)
{
	$data = file_get_contents($v);
	$data = str_replace('~', '', $data);

	preg_match_all('/<div class="rpt_b"><h3>([^~]*?)<div class="download">/', $data, $matches);

	$data = trim($matches[1][0]);
	
	$data = data_clean($data);//var_dump($data); exit(0);
	//var_dump($data); exit(0);
	return $data;
}

function get_content_for_tag($content, $b_tag, $e_tag)
{

}

function data_clean($data)
{
	$data = preg_replace('/<BR>/i', "\n", $data);
	$data = preg_replace('/<p>/i', "\n", $data);
	$data = str_replace("\n", '', $data);
	$data = str_replace('</div>', "\n", $data);
	$data = str_replace('★', "", $data);
	$data = str_replace('☆', "", $data);
	$data = str_replace('显示解析', "", $data);
	$data = preg_replace('/<img(.*?)src="(.*?)"[^~]*?>/', '[img]$2[/img][url=$2]请复制地址 看原图[/url] ', $data);
	$data = preg_replace('/<(.*?)>/i', '', $data);
	$data = str_replace('&nbsp;', ' ', $data);
	$data = str_replace('&ldquo;', '"', $data);
	$data = str_replace('&rdquo;', '"', $data);
	$data = str_replace('&ndash;', '"', $data);
	return $data;
}

function https_post($url, $data, $cookie='', $header=true)
{
	$curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
  //  curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  //  curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
    $header && curl_setopt($curl, CURLOPT_HEADER, 1); // 显示返回的Header区域内容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    $cookie && curl_setopt($curl, CURLOPT_COOKIE, $cookie);
	
	$tmpInfo = curl_exec($curl); // 执行操作
	if (curl_errno($curl)) {
       echo 'Errno'.curl_error($curl);//捕抓异常
    }
    curl_close($curl); // 关闭CURL会话
    return $tmpInfo; // 返回数据
}

?>